<template>
  <el-card>
    <el-table border style="margin: 10px 0" :data="skuArr">
      <el-table-column
        label="序号"
        type="index"
        align="center"
        width="80px"
      ></el-table-column>
      <el-table-column
        label="名称"
        show-overflow-tooltip
        prop="skuName"
      ></el-table-column>
      <el-table-column
        label="描述"
        show-overflow-tooltip
        prop="skuDesc"
      ></el-table-column>
      <el-table-column label="图片">
        <template #="{ row, $index }">
          <img
            :src="row.skuDefaultImg"
            alt=""
            style="width: 100px; height: 100px"
          />
        </template>
      </el-table-column>
      <el-table-column label="重量(kg)" prop="weight"></el-table-column>
      <el-table-column label="价格(元)" prop="price"></el-table-column>
      <el-table-column label="操作" fixed="right">
        <template #="{ row, $index }">
          <el-button
            type="primary"
            size="small"
            :icon="row.isSale === 0 ? 'Top' : 'Bottom'"
            @click="updateSale(row)"
          ></el-button>
          <el-button
            type="primary"
            size="small"
            icon="Edit"
            @click="updateSku"
          ></el-button>
          <el-button
            type="primary"
            size="small"
            icon="InfoFilled"
            @click="lookSku(row)"
          ></el-button>
          <el-popconfirm
            :title="`你确定删除${row.skuName}吗?`"
            @confirm="deleteSku(row)"
          >
            <template #reference>
              <el-button type="primary" size="small" icon="Delete"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 抽屉组件 展示商品详情 -->
    <el-drawer v-model="drawer">
      <template #header>
        <h4>查看商品详情</h4>
      </template>
      <template #default>
        <el-row style="margin: 10px 0">
          <el-col :span="6">名称</el-col>
          <el-col :span="18">{{ skuInfo.skuName }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0">
          <el-col :span="6">描述</el-col>
          <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0">
          <el-col :span="6">价格</el-col>
          <el-col :span="18">{{ skuInfo.price }}</el-col>
        </el-row>
        <el-row style="margin: 10px 0">
          <el-col :span="6">平台属性</el-col>
          <el-col :span="18">
            <el-tag
              v-for="item in skuInfo.skuAttrValueList"
              :key="item.id"
              style="margin: 5px"
              >{{ item.valueName }}</el-tag
            >
          </el-col>
        </el-row>
        <el-row style="margin: 10px 0">
          <el-col :span="6">销售属性</el-col>
          <el-col :span="18">
            <el-tag
              v-for="item in skuInfo.skuSaleAttrValueList"
              :key="item.id"
              style="margin: 5px"
              >{{ item.saleAttrValueName }}</el-tag
            >
          </el-col>
        </el-row>
        <el-row style="margin: 10px 0">
          <el-col :span="6">商品图片</el-col>
          <el-col :span="18">
            <el-carousel :interval="4000" type="card" height="200px">
              <el-carousel-item
                v-for="item in skuInfo.skuImageList"
                :key="item.id"
              >
                <img
                  :src="item.imgUrl"
                  alt=""
                  style="width: 100%; height: 100%"
                />
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </template>
    </el-drawer>
    <!-- 分页器 -->
    <el-pagination
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :page-sizes="[3, 5, 7, 9]"
      :background="true"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="total"
      @change="getHasSku"
    />
  </el-card>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import {
  reqSkuList,
  reqSaleSku,
  reqCancelSale,
  reqSkuInfo,
  reqDeleteSku,
} from "@/api/product/sku";
import { ElMessage } from "element-plus";
import { useImaStore } from "@/store/modules/img";
const imgStore = useImaStore();
// 分页器默认页码
const pageNo = ref<number>(1);
// 每一页默认展示几条数据
const pageSize = ref<number>(5);
// 存储数据总数
const total = ref<number>(0);
// 存储SKU数据
const skuArr = ref<any>([]);
// 控制抽屉的显示与隐藏
const drawer = ref<boolean>(false);

// 存储商品详情
const skuInfo = ref<any>({});

// 获取数据方法封装
const getHasSku = async (pager = 1) => {
  // 获取当前分页器页码
  pageNo.value = pager;
  const res = await reqSkuList(pageNo.value, pageSize.value);
  if (res.code === 200) {
    total.value = res.data.total;
    skuArr.value = res.data.records;
  }
};

// 上下架SKU
const updateSale = async (row: any) => {
  // 如果isSale为1 则代表商品处于上架状态
  if (row.isSale) {
    const res = await reqSaleSku(row.id);
    if (res.code === 200) {
      row.isSale = 0;
      ElMessage.success("下架成功");
    }
  } else {
    const res = await reqCancelSale(row.id);
    if (res.code === 200) {
      row.isSale = 1;
      ElMessage.success("上架成功");
    }
  }
};

// 编辑SKU
const updateSku = () => {
  ElMessage.success("真写累了");
};

// 查看商品详情
const lookSku = async (row: any) => {
  drawer.value = true;
  const res = await reqSkuInfo(row.id);

  if (res.code === 200) {
    skuInfo.value = res.data;
    // 从仓库中取出照片数据
    if (imgStore.imgList.length > 0) {
      skuInfo.value.skuImageList = imgStore.imgList;
    }
  }
};

// 删除SKU
const deleteSku = async (row: any) => {
  const res = await reqDeleteSku(row.id);
  if (res.code === 200) {
    ElMessage.success("删除成功");
    getHasSku(skuArr.value.length > 1 ? pageNo.value : pageNo.value - 1);
  } else {
    ElMessage.error("删除失败");
  }
};

// 组件挂载完毕后发送请求
onMounted(() => {
  getHasSku();
});
</script>

<style scoped>
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
